import { createRouter, createWebHistory } from "vue-router"
import { localCache } from "@/utils/cache"
import { fristMenu } from "@/utils/map-menus"
const LOGIN_TOKEN = "login/token"

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/main"
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/login.vue")
    },
    {
      path: "/main",
      name: "main",
      component: () => import("../views/main/main.vue"),
      children: [
        // {path:"/main/analysis/dashboard",name:"dashboard",component:()=>import("../views/main/analysis/dashboard/dashboard.vue")},
        // {path:"/main/analysis/overview",name:"overview",component:()=>import("../views/main/analysis/overview/overview.vue")},
        // {path:"/main/system/user",name:"user",component:()=>import("../views/main/system/user/user.vue")},
        // {path:"/main/system/role",name:"role",component:()=>import("../views/main/system/role/role.vue")},
        // {path:"/main/system/menu",name:"menu",component:()=>import("../views/main/system/menu/menu.vue")},
        // {path:"/main/system/department",name:"department",component:()=>import("../views/main/system/department/department.vue")},
      ]
    },
    {
      path: "/:pathMatch(.*)",
      component: () => import("../views/not-found/NotFound.vue")
    }
  ]
})
//用户进行刷新：
const localRoute = [
  {
    path: "/main/analysis/dashboard",
    name: "dashboard",
    component: () => import("../views/main/analysis/dashboard/dashboard.vue")
  },
  {
    path: "/main/analysis/overview",
    name: "overview",
    component: () => import("../views/main/analysis/overview/overview.vue")
  }
]
// 动态添加路由
// router.addRoute("main",localRoute[0])

//导航守卫
//返回值决定返回的路径
//不返回或者返回undefined
//
router.beforeEach((to, from) => {
  const token = localCache.getCache(LOGIN_TOKEN)
  if (to.path === "/main" && !token) {
    return "/login"
  }
  if(to.path==="/main"){
    return fristMenu?.url
  }
})
export default router
